//
//  SLSpecialOffersViewController.m
//  StarLight
//
//  Created by apple on 16/8/12.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLSpecialOffersViewController.h"
#import "SLSpecialOffersCell.h"
#import "SLSpecialOffersViewModel.h"
#import "BKJH5ViewController.h"

@interface SLSpecialOffersViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic ,strong) UICollectionView *collectionView;
@property (nonatomic ,strong) SLSpecialOffersViewModel *requestModel;


@end

@implementation SLSpecialOffersViewController

- (instancetype)init{
    if (self = [super init]) {
        self.requestModel = [[SLSpecialOffersViewModel alloc] init];
    }return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addConstrains];
    [self addObservers];
    // Do any additional setup after loading the view.
}
- (void)addObservers{
    @weakify(self)
    [[RACObserve(self.requestModel, listArr) filter:^BOOL(NSMutableArray *value) {
        return value != nil;
    }] subscribeNext:^(NSMutableArray *list) {
        @strongify(self)
        [self.collectionView reloadData];
    }];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.title = @"特惠活动";
    self.view.backgroundColor = RGBCOLOR(225, 225, 230);
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}
#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 1;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.requestModel.listArr.count;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10 , 10, 0, 10);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(SCREEN_WIDTH - 20, 220 * MIX_H);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SLSpecialOffersCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = RGBCOLOR(255, 255, 255);
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(SLSpecialOffersCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
    [cell cellForData:self.requestModel.listArr[indexPath.section]];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SLSpecialOffersModel *model = self.requestModel.listArr[indexPath.section];
    BKJH5ViewController *h5View = [[BKJH5ViewController alloc] init];
    h5View.h5Url = model.contentUrl;
    [self.navigationController pushViewController:h5View animated:YES];
}

#pragma mark - addUI
- (void)addConstrains{
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.width.equalTo(self.view);
        make.bottom.equalTo(self.mas_bottomLayoutGuideBottom);
        make.centerX.equalTo(self.view);
    }];
}
#pragma mark - getter
- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        [_collectionView registerClass:[SLSpecialOffersCell class] forCellWithReuseIdentifier:@"cell"];
        _collectionView.backgroundColor = RGBCOLOR(225, 225, 230);
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
    }
    return _collectionView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
